package cn.com.cym.gamezao.base;


import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import java.time.LocalDateTime;


/**
 * 实体类基类
 * MappedSuperclass 该注解是告诉启动器在启动的时候，不会去映射到数据库表，只有子类会映射到表，有该注解不能再标注注解@Table和@Entity
 * JsonIgnoreProperties 该注解处理在前端发起数据无法支持序列化问题 解决 No serializer found for class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor and no propert 问题
 */
@ApiModel(value = "实体类基类",description = "实体类基类，所有该项目下的POJO都需要继承该类")
@Getter
@Setter
@NoArgsConstructor
@MappedSuperclass
@Accessors(chain = true)
@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" })
public class BaseEntity implements Comparable<BaseEntity>{


    /**
     * 唯一id，使用mysql自增长
     */
    @Id
    @Column
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    @ApiModelProperty(name = "id",value = "uniqueId",dataType = "Long")
    private Long id;


    /**
     * 创建时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @ApiModelProperty(name = "createTime",value = "创建时间",dataType = "LocalDateTime")
    private LocalDateTime createTime;


    /**
     * 创建人id  如果为-1 作为匿名创建，只有用户注册才是
     */
    @ApiModelProperty(name = "createUserId",value = "创建人id  如果为-1 作为匿名创建，只有用户注册才是",dataType = "Long")
    private Long createUserId;

    /**
     * 修改时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @ApiModelProperty(name = "modifyTime",value = "修改时间",dataType = "LocalDateTime")
    private LocalDateTime modifyTime;


    /**
     * 修改人id
     */
    @ApiModelProperty(name = "modifyUserId",value = "修改人id  如果为-1 作为匿名创建，只有用户注册才是",dataType = "Long")
    private Long modifyUserId;


    /**
     * 是否删除
     */
    @ApiModelProperty(name = "deleted",value = "是否删除",dataType = "Boolean")
    private Boolean deleted=false;


    /**
     * 版本信息 如果版本没有更新 不做处理
     */
    @ApiModelProperty(name = "versionCode",value = "版本信息 如果版本没有更新 不做处理",dataType = "Long")
    private Long versionCode=0L;


    /**
     * 排序
     */
    @ApiModelProperty(name = "sortNo",value = "排序",dataType = "Long")
    private Long sortNo=0L;

    @Override
    public String toString() {
        return "BaseEntity{" +
                "id=" + id +
                ", createTime=" + createTime +
                ", createUserId=" + createUserId +
                ", modifyTime=" + modifyTime +
                ", modifyUserId=" + modifyUserId +
                ", isDeleted=" + deleted +
                ", versionCode=" + versionCode +
                '}';
    }

    @Override
    public int compareTo(BaseEntity o) {
        if(this.getSortNo()==null || o.getSortNo()==null){
            return 0;
        }else if(this.getSortNo()>=o.getSortNo()){
            return 1;
        }else{
            return -1;
        }
        //return 0;
    }
}
